"""
菜单栏组件
负责主窗口的菜单和工具栏
"""

from PySide6.QtWidgets import QMenuBar, QMenu, QToolBar, QFileDialog, QMessageBox
from PySide6.QtCore import Signal, QObject
from PySide6.QtGui import QAction, QKeySequence


class MenuBarWidget(QObject):
    """
    菜单栏组件
    
    功能：
    - 创建主菜单
    - 创建工具栏
    - 处理菜单事件
    """
    
    # 信号定义
    openImageRequested = Signal(str)
    openVideoRequested = Signal(str)
    openCameraRequested = Signal(int)
    settingsRequested = Signal()
    aboutRequested = Signal()
    exitRequested = Signal()
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        
    def create_menu_bar(self):
        """创建菜单栏"""
        menu_bar = QMenuBar(self.parent)
        
        # 文件菜单
        file_menu = menu_bar.addMenu("文件(&F)")
        
        # 打开图片
        open_image_action = QAction("打开图片(&I)", self.parent)
        open_image_action.setShortcut(QKeySequence.Open)
        open_image_action.triggered.connect(self._open_image)
        file_menu.addAction(open_image_action)
        
        # 打开视频
        open_video_action = QAction("打开视频(&V)", self.parent)
        open_video_action.setShortcut("Ctrl+Shift+O")
        open_video_action.triggered.connect(self._open_video)
        file_menu.addAction(open_video_action)
        
        # 打开摄像头
        open_camera_action = QAction("打开摄像头(&C)", self.parent)
        open_camera_action.setShortcut("Ctrl+Alt+C")
        open_camera_action.triggered.connect(self._open_camera)
        file_menu.addAction(open_camera_action)
        
        file_menu.addSeparator()
        
        # 退出
        exit_action = QAction("退出(&X)", self.parent)
        exit_action.setShortcut(QKeySequence.Quit)
        exit_action.triggered.connect(self.exitRequested.emit)
        file_menu.addAction(exit_action)
        
        # 设置菜单
        settings_menu = menu_bar.addMenu("设置(&S)")
        
        # 配置
        config_action = QAction("配置(&C)", self.parent)
        config_action.setShortcut("Ctrl+,")
        config_action.triggered.connect(self.settingsRequested.emit)
        settings_menu.addAction(config_action)
        
        # 帮助菜单
        help_menu = menu_bar.addMenu("帮助(&H)")
        
        # 关于
        about_action = QAction("关于(&A)", self.parent)
        about_action.triggered.connect(self.aboutRequested.emit)
        help_menu.addAction(about_action)
        
        return menu_bar
        
    def create_tool_bar(self):
        """创建工具栏"""
        toolbar = QToolBar("主工具栏", self.parent)
        toolbar.setObjectName("MainToolBar")
        
        # 打开图片
        open_image_action = QAction("打开图片", self.parent)
        open_image_action.setToolTip("打开图片文件进行检测")
        open_image_action.triggered.connect(self._open_image)
        toolbar.addAction(open_image_action)
        
        # 打开视频
        open_video_action = QAction("打开视频", self.parent)
        open_video_action.setToolTip("打开视频文件进行检测")
        open_video_action.triggered.connect(self._open_video)
        toolbar.addAction(open_video_action)
        
        # 打开摄像头
        open_camera_action = QAction("打开摄像头", self.parent)
        open_camera_action.setToolTip("打开摄像头进行实时检测")
        open_camera_action.triggered.connect(self._open_camera)
        toolbar.addAction(open_camera_action)
        
        toolbar.addSeparator()
        
        # 设置
        settings_action = QAction("设置", self.parent)
        settings_action.setToolTip("打开设置对话框")
        settings_action.triggered.connect(self.settingsRequested.emit)
        toolbar.addAction(settings_action)
        
        return toolbar
        
    def _open_image(self):
        """打开图片文件"""
        file_path, _ = QFileDialog.getOpenFileName(
            self.parent,
            "选择图片文件",
            "",
            "图片文件 (*.jpg *.jpeg *.png *.bmp *.tiff);;所有文件 (*)"
        )
        
        if file_path:
            self.openImageRequested.emit(file_path)
            
    def _open_video(self):
        """打开视频文件"""
        file_path, _ = QFileDialog.getOpenFileName(
            self.parent,
            "选择视频文件",
            "",
            "视频文件 (*.mp4 *.avi *.mov *.mkv *.wmv);;所有文件 (*)"
        )
        
        if file_path:
            self.openVideoRequested.emit(file_path)
            
    def _open_camera(self):
        """打开摄像头"""
        # 默认使用摄像头0
        self.openCameraRequested.emit(0)
        
    def show_about(self):
        """显示关于对话框"""
        QMessageBox.about(
            self.parent,
            "关于",
            "实时车牌识别系统\n\n"
            "版本: 1.0.0\n"
            "基于 YOLOv5 + PaddleOCR 实现\n"
            "使用 PySide6 构建界面\n\n"
            "功能特性:\n"
            "• 支持图片、视频、摄像头输入\n"
            "• 实时车牌检测和识别\n"
            "• 结果导出和统计\n"
            "• 可配置的检测参数"
        ) 